import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DeleteDemo {
    public static void main(String[] args) {
        int rs = DeleteDemo.delete();
        if(rs > 0){
            System.out.println("删除记录成功！！！");
        }else{
            System.out.println("删除记录失败！！！");
        }
    }

    private static int delete() {
        int result = 0;
        Connection connection = null;
        PreparedStatement pstmt = null;
        // 1.加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2.获取数据库连接
            String url = "jdbc:mysql://localhost:3306/jdbctest?zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai";
            connection = DriverManager.getConnection(url, "root", "root");
            // 3.1sql语句
            String sql = "delete from user where username = '唐三'";
            // 3.2获取执行sql语句的对象
            pstmt = connection.prepareStatement(sql);
            // 3.3执行sql
            result = pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                pstmt = null;
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
        return result;
    }
}
